package com.wtx.chapter02;

/**
 * @description: 自己的数组类
 * @author: wtx
 * @createDate: 2020/5/13
 */
public class Array<E> {

    private E[] data;
    private int size;
    //不必设置一个capacity成员，可以直接由data.length获得

    public Array(int capacity){
        //不能直接new E[]
        data = (E[]) new Object[capacity];
        size = 0;
    }

    public Array() {
        //默认为容量为10
        this(10);
    }
    public int getSize(){
        return this.size;
    }
    public int getCapacity(){
        return this.data.length;
    }

    public boolean isEmpty(){
        return size==0;
    }

    //O(1)
    public void addLast(E e){

        //可以代码复用
        add(size,e);

        /*if (size == data.length)
            throw new IllegalArgumentException("Array is full");
        //不要写成data[size++] = e，一行代码应该只进行一项工作
        data[size] = e;
        size++;*/
    }

    //O(n)
    public void addFirst(E e){
        add(0,e);
    }

    // 平均O(n/2)，最坏O(n)
    public void add(int index,E e){
        /*if (size == data.length)
            throw new IllegalArgumentException("Array is full");*/
        // index > size 不是 > capacity , 这是为了保证数组的每一个空间都得到利用。
        if(index < 0 || index > size)
            throw new IllegalArgumentException("Require index >= 0 and index <= size");

        if (size == data.length)
            resize( 2 * data.length);

        for (int i = size-1; i >= index; i--)
            data[i+1] = data[i];
        data[index] = e;
        size++;
    }

    //O(1)
    public E get(int index){

        if( index < 0 || index >= size )
            throw new IllegalArgumentException("Index is illegal");
        return data[index];
    }

    //O(1),数组支持随机访问
    public void set(int index,E e){

        if( index < 0 || index >= size )
            throw new IllegalArgumentException("Index is illegal");
        data[index] = e;
    }

    //最坏O(n)
    public boolean contains(E e){

        for (E d: data) {
            //对于一个对象，==是进行引用比较，equals()是进行值比较
            if(d.equals(e))
                return true;
        }
        return false;
    }

    //O(n)
    //不存在返回-1
    public int find(E e){

        for (int i = 0; i < size; i++) {
            if (data[i]==e)
                return i;
        }
        return -1;
    }

    //最坏O(n)
    public E remove(int index){
        if( index < 0 || index >= size )
            throw new IllegalArgumentException("Index is illegal");
        E ret = data[index];

        for (int i = index+1; i <size ; i++)
            data[i-1] = data[i];

        size--;
        //java自动回收机制会回收没有引用的对象
        data[size] = null;

        //节约空间，数组可以实现动态伸缩
        if ( size == data.length / 4 && data.length / 2 != 0)
            //潜在的bug：数组的容量可能会被缩容为0, 在if条件中解决
            resize(data.length / 2);
        return ret;
    }

    public E removeFirst(){
        return remove(0);
    }

    public E removeLast(){
        return remove(size-1);
    }

    public void removeElement(E e){

        int index = find(e);
        if(index != -1)
            remove(index);
    }

    @Override
    public String toString() {

        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d, capacity = %d\n",size,data.length));
        res.append("[");
        for ( E d : data) {
            res.append(d);
            res.append(" ");
        }
        res.append("]");
        return res.toString();
    }

    private void resize(int capacity){
        E[] newData = (E[]) new Object[capacity];

        for (int i = 0; i < size; i++)
            newData[i] = data[i];

        data = newData;
    }
}